1
|
|
|
/*! |
2
|
|
|
* byte-data |
3
|
|
|
* Readable data to and from byte buffers. |
4
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
5
|
|
|
* https://github.com/rochars/byte-data |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
let toBytes = require('./src/to-bytes'); |
10
|
|
|
let fromBytes = require('./src/from-bytes'); |
11
|
|
|
let bitPacker = require('./src/bit-packer'); |
12
|
|
|
let bitDepth = require('./src/bit-depth'); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Find and return the start index of some string. |
16
|
|
|
* Return -1 if the string is not found. |
17
|
|
|
* @param {!Array<number>|Uint8Array} bytes Array of bytes. |
18
|
|
|
* @param {string} chunk Some string to look for. |
19
|
|
|
* @return {number} The start index of the first occurrence, -1 if not found |
20
|
|
|
*/ |
21
|
|
|
function findString(bytes, chunk) { |
22
|
|
|
let found = ""; |
23
|
|
|
for (let i = 0; i < bytes.length; i++) { |
24
|
|
|
found = fromBytes.fromBytes(bytes.slice(i, i + chunk.length), |
25
|
|
|
8, {"char": true}); |
26
|
|
|
if (found == chunk) { |
27
|
|
|
return i; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
return -1; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Turn a number or string into a byte buffer. |
35
|
|
|
* @param {number|string} value The value. |
36
|
|
|
* @param {Object} type One of the available types. |
37
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
38
|
|
|
* @return {!Array<number>|!Array<string>} |
39
|
|
|
*/ |
40
|
|
|
function pack(value, type, base=10) { |
41
|
|
|
let theType = Object.assign({}, type); |
42
|
|
|
theType.base = base; |
43
|
|
|
theType.single = true; |
44
|
|
|
value = theType.char ? value[0] : value; |
45
|
|
|
return toBytes.toBytes(value, theType.bitDepth, theType); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Turn a byte buffer into a readable value. |
50
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes. |
51
|
|
|
* @param {Object} type One of the available types. |
52
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
53
|
|
|
* @return {number|string} |
54
|
|
|
*/ |
55
|
|
|
function unpack(buffer, type, base=10) { |
56
|
|
|
let theType = Object.assign({}, type); |
57
|
|
|
theType.base = base; |
58
|
|
|
theType.single = true; |
59
|
|
|
return fromBytes.fromBytes(buffer, theType.bitDepth, theType); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Turn a array of numbers into a byte buffer. |
64
|
|
|
* @param {!Array<number>} values The values. |
65
|
|
|
* @param {Object} type One of the available types. |
66
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
67
|
|
|
* @return {!Array<number>|!Array<string>} |
68
|
|
|
*/ |
69
|
|
|
function packArray(values, type, base=10) { |
70
|
|
|
let theType = Object.assign({}, type); |
71
|
|
|
theType.base = base; |
72
|
|
|
theType.single = false; |
73
|
|
|
return toBytes.toBytes(values, theType.bitDepth, theType); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Turn a byte array into a sequence of readable values. |
78
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte array. |
79
|
|
|
* @param {Object} type One of the available types. |
80
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
81
|
|
|
* @return {!Array<number>|string} |
82
|
|
|
*/ |
83
|
|
|
function unpackArray(buffer, type, base=10) { |
84
|
|
|
let theType = Object.assign({}, type); |
85
|
|
|
theType.base = base; |
86
|
|
|
theType.single = false; |
87
|
|
|
return fromBytes.fromBytes(buffer, theType.bitDepth, theType); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// interface |
91
|
|
|
module.exports.pack = pack; |
92
|
|
|
module.exports.unpack = unpack; |
93
|
|
|
module.exports.packArray = packArray; |
94
|
|
|
module.exports.unpackArray = unpackArray; |
95
|
|
|
|
96
|
|
|
// types |
97
|
|
|
module.exports.chr = {"bitDepth": 8, "char": true, "single": true}; |
98
|
|
|
module.exports.bool = {"bitDepth": 1, "single": true}; |
99
|
|
|
module.exports.int2 = {"bitDepth": 2, "signed": true, "single": true}; |
100
|
|
|
module.exports.uInt2 = {"bitDepth": 2, "single": true}; |
101
|
|
|
module.exports.int4 = {"bitDepth": 4, "signed": true, "single": true}; |
102
|
|
|
module.exports.uInt4 = {"bitDepth": 4, "single": true}; |
103
|
|
|
module.exports.int8 = {"bitDepth": 8, "signed": true, "single": true}; |
104
|
|
|
module.exports.uInt8 = {"bitDepth": 8, "single": true}; |
105
|
|
|
module.exports.int16 = {"bitDepth": 16, "signed": true, "single": true}; |
106
|
|
|
module.exports.uInt16 = {"bitDepth": 16, "single": true}; |
107
|
|
|
module.exports.float16 = {"bitDepth": 16, "float": true, "single": true}; |
108
|
|
|
module.exports.int24 = {"bitDepth": 24, "signed": true, "single": true}; |
109
|
|
|
module.exports.uInt24 = {"bitDepth": 24, "single": true}; |
110
|
|
|
module.exports.int32 = {"bitDepth": 32, "signed": true, "single": true}; |
111
|
|
|
module.exports.uInt32 = {"bitDepth": 32, "single": true}; |
112
|
|
|
module.exports.float32 = {"bitDepth": 32, "float": true, "single": true}; |
113
|
|
|
module.exports.int40 = {"bitDepth": 40, "signed": true, "single": true}; |
114
|
|
|
module.exports.uInt40 = {"bitDepth": 40, "single": true}; |
115
|
|
|
module.exports.int48 = {"bitDepth": 48, "signed": true, "single": true}; |
116
|
|
|
module.exports.uInt48 = {"bitDepth": 48, "single": true}; |
117
|
|
|
module.exports.float64 = {"bitDepth": 64, "float": true, "single": true}; |
118
|
|
|
|
119
|
|
|
module.exports.findString = findString; |
120
|
|
|
module.exports.toBytes = toBytes.toBytes; |
121
|
|
|
module.exports.fromBytes = fromBytes.fromBytes; |
122
|
|
|
module.exports.packBooleans = bitPacker.packBooleans; |
123
|
|
|
module.exports.unpackBooleans = bitPacker.unpackBooleans; |
124
|
|
|
module.exports.packCrumbs = bitPacker.packCrumbs; |
125
|
|
|
module.exports.unpackCrumbs = bitPacker.unpackCrumbs; |
126
|
|
|
module.exports.packNibbles = bitPacker.packNibbles; |
127
|
|
|
module.exports.unpackNibbles = bitPacker.unpackNibbles; |
128
|
|
|
module.exports.BitDepthOffsets = bitDepth.BitDepthOffsets; |
129
|
|
|
module.exports.BitDepthMaxValues = bitDepth.BitDepthMaxValues; |
130
|
|
|
|